import { MockMethod } from "vite-plugin-mock";

const skuList = [
  {
    id: 1,
    prodId: 1,
    name: "xxx",
    pic: "https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif",
    attribute: '[{"颜色": "红色"},{"运存":"8G"},{"内存":"128G"}]',
    supplierPrice: 1700,
    salePrice: 2000,
    referencePrice: 2200,
    remained: 200,
  },
  {
    id: 2,
    prodId: 1,
    name: "yyy",
    pic: "https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif",
    attribute: '[{"颜色": "红色"},{"运存":"12G"},{"内存":"256G"}]',
    supplierPrice: 2000,
    salePrice: 2400,
    referencePrice: 2600,
    remained: 200,
  },
  {
    id: 3,
    prodId: 2,
    name: "zzz",
    pic: "https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif",
    attribute: '[{"颜色": "红色"}]',
    supplierPrice: 1700,
    salePrice: 2000,
    referencePrice: 2200,
    remained: 200,
  },
  {
    id: 4,
    prodId: 3,
    name: "fff",
    pic: "https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif",
    attribute: '[{"颜色": "红色"}]',
    supplierPrice: 1700,
    salePrice: 2000,
    referencePrice: 2200,
    remained: 200,
  },
  {
    id: 5,
    prodId: 4,
    name: "uuu",
    pic: "https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif",
    attribute: '[{"颜色": "红色"}]',
    supplierPrice: 1700,
    salePrice: 2000,
    referencePrice: 2200,
    remained: 200,
  },
];

const mock: Array<MockMethod> = [
  {
    //2.选择商品, 获取sku列表接口
    url: "/dev-api/api/v1/prod/page",
    statusCode: 200,
    method: "get",
    response: (config: any) => {
      // console.log(config);
      let query = config.query;
      let pageNum = query.pageNum;
      let pageSize = query.pageSize;

      // (当前页 - 1) * 分页大小 = 开始索引
      let startIndex: number = (pageNum - 1) * pageSize;

      // 当前页 * 分页大小 > (skuList.length - 1) ? skuList.length : 当前页 * 分页大小   = 结束索引
      let endIndex: number =
        pageNum * pageSize > skuList.length - 1
          ? skuList.length
          : pageNum * pageSize;

      return {
        code: "00000",
        msg: "ok",
        data: {
          list: skuList.slice(startIndex, endIndex),
          total: skuList.length,
        },
      };
    },
  },

  {
    //2.选择商品, 获取sku列表接口
    url: "/dev-api/api/v1/sku/info",
    statusCode: 200,
    method: "get",
    response: (config: any) => {
      // console.log(config);
      let skuIds: number[] = config.query.skuIds; //需要用qs处理下axios的参数, 这里才能这样获取到参数里的 数组
      // console.log(skuIds);
      if (!(skuIds instanceof Array)) {
        skuIds = [...skuIds];
      }

      return {
        code: "00000",
        msg: "ok",
        data: {
          list: skuList.filter((sku) => skuIds.find((id) => id == sku.id)),
          total: skuList.length,
        },
      };
    },
  },
];

export default mock;
